package com.aidex.biz.controller;

import com.aidex.biz.domain.Taizhang001;
import com.aidex.biz.service.Taizhang001Service;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.AjaxResult;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 婚丧喜庆台账Controller
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-05-21
 */
@RestController
@RequestMapping("/biz/bizTaizhang001")
public class Taizhang001Controller extends BaseController {
    @Autowired
    private Taizhang001Service taizhang001Service;

    /**
     * 查询婚丧喜庆台账列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:list')")
    @GetMapping("/list")
    public R<PageInfo> list(Taizhang001 taizhang001, HttpServletRequest request, HttpServletResponse response) {
        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            taizhang001.setCompanyId(null);
        } else {
            taizhang001.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        taizhang001.setPage(new PageDomain(request, response));
        return R.data(taizhang001Service.findPage(taizhang001));
    }

    /**
     * 获取婚丧喜庆台账详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:query')")
    @GetMapping(value = "/{id}")
    public R<Taizhang001> detail(@PathVariable("id") String id) {
        return R.data(taizhang001Service.get(id));
    }

    /**
     * 新增婚丧喜庆台账
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:add')")
    @Log(title = "婚丧喜庆台账", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Taizhang001 taizhang001) {
        return R.status(taizhang001Service.save(taizhang001));
    }

    /**
     * 修改婚丧喜庆台账
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:edit')")
    @Log(title = "婚丧喜庆台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Taizhang001 taizhang001) {
        return R.status(taizhang001Service.save(taizhang001));
    }


    /**
     * 删除婚丧喜庆台账
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:remove')")
    @Log(title = "婚丧喜庆台账", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.status(taizhang001Service.deleteTaizhang001ByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort() {
        return R.data(taizhang001Service.findMaxSort(new Taizhang001()));
    }

    /**
     * 导出婚丧喜庆台账列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizTaizhang001:export')")
    @Log(title = "婚丧喜庆台账", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Taizhang001 taizhang001) {

        if (SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            taizhang001.setCompanyId(null);
        } else {
            taizhang001.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        }
        List<Taizhang001> list = taizhang001Service.findList(taizhang001);
        ExcelUtil<Taizhang001> util = new ExcelUtil<Taizhang001>(Taizhang001.class);
        return util.exportExcel(list, "婚丧喜庆台账数据");
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<Taizhang001> util = new ExcelUtil<Taizhang001>(Taizhang001.class);
        List<Taizhang001> list = util.importExcel(file.getInputStream());

        String message = taizhang001Service.importData(list);
        return AjaxResult.success(message);
    }

}
